package com.neu.dao.snn;

import com.neu.dao.BaseDao;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class UserDaoImpl extends BaseDao {
    //上传头像
    public int save(Object...objects){
        int row=0;
        String sql="update t_user set u_img=? where u_id=?";
        try {
            row=getQueryRunner().update(sql,objects);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }
    //查看已付款和交易完成的订单
    public List<Map<String,Object>> findAllById(Object...objects){
        List<Map<String,Object>> list = null;
        String sql="select * from t_order o,t_address a,t_order_info oi" +
                " where o.a_id=a.a_id" +
                "  and  o.o_id=oi.o_id" +
                " and o.u_id=?";
        try {
            list=getQueryRunner().query(sql,new MapListHandler(),objects);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }


    //查询我的消费情况
    public List<Map<String,Object>> findById(Object...objects){
        List<Map<String,Object>> list = null;
        String sql="select sum(o_price) o_price,o_date from t_order where u_id=? group by o_date";
        try {
            list=getQueryRunner().query(sql,new MapListHandler(),objects);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    //用户修改信息
    public int editUserInfo(Object...objects){
        int row=0;
        String sql="update t_user set u_name=?,u_email=?,u_sex=?,u_birth=to_date(?,'yyyy-mm-dd') where u_id=?";
        try {
            row=getQueryRunner().update(sql,objects);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }

    //用户修改密码
    public int editPwd(Object...objects) {
        int row=0;
        String sql="update t_user"
                + " set u_pwd=?"
                + " where u_id=?"
                + " and u_pwd=?";
        try {
            row=getQueryRunner().update(sql,objects);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }

    //添加收货地址
    public int addAddress(Object...objects){
        int row=0;
        String sql = "insert into t_address(a_id,u_id,a_add,a_name,a_tel)" +
                " values(seq_id.nextval,?,?,?,?)";
        try {
            row=getQueryRunner().update(sql,objects);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }
    //修改已保存的地址
    public  int updateAddress(Object...objects){
        int row=0;
        String sql="update t_address set a_name=?,a_add=?,a_tel=? where a_id=?";
        System.out.println(sql);
        System.out.println(Arrays.toString(objects));
        try {
            row = getQueryRunner().update(sql,objects);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  row;
    }

    //查询已添加的收货地址
    public  List<Map<String,Object>> findAddressById(Object...objects){
        List<Map<String,Object>> list = null;
        String sql = "select a_id, a_name,a_add,a_tel from t_address where u_id=?";
        try {
            list=getQueryRunner().query(sql,new MapListHandler() ,objects);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    //删除已添加的收货地址
    public int deleteAddress(Object...objects){
        int row =0;
        String sql="delete from t_address where a_id=?";
        try {
            row=getQueryRunner().update(sql,objects);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }
    //重新查询用户信息
    public List<Map<String,Object>> findUserById(Object...objs){
        List<Map<String,Object>> list=null;
        String sql="select * from t_user where u_id=?";
        try {
            list=getQueryRunner().query(sql,new MapListHandler(),objs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    //管理员查询用户信息
    public List<Map<String,Object>> mgrFindUser(){
        List<Map<String,Object>> list = null;
        String sql="select u_name,u_tel,u_email,u_sex,u_birth from t_user";
        try {
            list=getQueryRunner().query(sql,new MapListHandler());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    //用户信息分页
    //获取总页数
    public List<Map<String,Object>> findTitleNum(int pageCount){
        List<Map<String,Object>> list=null;
        String sql="select ceil(count(u_id)/?) a from t_user";
        try {
            list=getQueryRunner().query(sql,new MapListHandler(),pageCount);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    //获取每一页显示的数据
    public List<Map<String,Object>> findByPage(int pageIndex,int pageCount){
        List<Map<String,Object>> list=null;
        int end=pageIndex*pageCount;
        int begin=(pageIndex-1)*pageCount;
        String sql="select * from " +
                " (select a.*,rownum r from " +
                " (select * from t_user order by u_id desc) a" +
                " where rownum<=?) b" +
                " where b.r>?";
        try {
            list=getQueryRunner().query(sql,new MapListHandler(),end,begin);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

}
